	const SR="SR";
	const SF="SF";
	const RT="RT";
	const LG="LG";
	const DS="DS";

	const LgVerbs = new Array("begin", "finish", "cancel");
	const LgVerbsPast = new Array("began", "finished", "canceled");
	
parseTweetflow = function(tweet){
	
	
	
	var words = [];
	
	var parsedTweetflow;
	
	parseWords = function(){
		words=tweet.split(' ');
	}
	
	determineType = function(){
		if(words.length > 0){
			var type=words[0];
			if(type == SR || type == RT || type==DS)
			{
				parsedTweetflow = new SRTweetflow(type);
			}
			else if(type== SF || type==LG)
			{
				parsedTweetflow = new SFTweetflow(type);
			}
		}
	}
	
	parseSR = function(){
		//words[]=SR [@user]? operation.service input [condition]?
		//        0     1              2          3        4
		var pos=1;
		parseUser(words[pos]);
		
		if(parsedTweetflow.user != undefined){
			// user given -> go to next word
			pos++;
		}
		
		parseOperationService(words[pos]);
		pos++;
		parseInput(words[pos]);
		pos++;
		parseCondition(words[pos]);
	};
	
	parseUser= function(text){
		if(text && typeof(text)=='string'){
			if(text.indexOf('@') == 0){
				parsedTweetflow.user=text;
			}
		}
	};
	
	parseOperationService= function(text){
		//operation.service
		if(text && typeof(text)=='string'){
		   var os = text.split(".");
		   if(os.length==2){
		   		parsedTweetflow.operation=os[0];
		   		parsedTweetflow.service=os[1];
		   }
		   else{
		   	// tweetflow defacto invalid!
		   	    parsedTweetflow.type = undefined;
		   }
		}
	}
	
	parseInput = function(text){
		if(text && typeof(text)=='string'){
			parsedTweetflow.input=text;
		}
	}
	
	parseCondition = function(text){
		//condition = [name comparator value]
		if(text && typeof(text)=='string' && text.indexOf('#')<0){
			parsedTweetflow.condition=text;
		}
	}
	
	parseSF= function(){
		//words[]=SF @requester "did"operation.service [output]?
		//        0     1                     2            3
		parseUser(words[1]);
		parseDidOperationService(words[2]);
		parseOutput(words[3]);
	};
	
	parseLG= function(){
		//words[]=LG @requester "did"[begin|finish|cancel]operation.service [inoutput] [expression] #hashtags
		//        0     1                        2                               3          4           5
		console.log("parsing LG");
		parseUser(words[1]);
		parseDidLogOperationService(words[2]);
		parseOutput(words[3]);
		parseCondition(words[4]);
	}
	
	parseDidOperationService= function(text){
		//"did"operation.service
		if(text && typeof(text)=='string'){
		   var os = text.split(".");
		   if(os.length==2){
		   		if(os[0].indexOf("did") == 0){
		   			parsedTweetflow.didOperation=os[0].substr(3);
		   		}
		   		parsedTweetflow.service=os[1];
		   }
		}
	}
	
	parseDidLogOperationService= function(text){
		//"did"[begin|finish|cancel]operation.service
		
		if(text && typeof(text)=='string'){
		   var os = text.split(".");
		   if(os.length==2){
		   		if(os[0].indexOf("did") == 0){
		   			var operation=os[0].substr(3);
		   			
		   			// check for all defined verbs
		   			for (var i = LgVerbs.length - 1; i >= 0; i--){
						 var opWoVerb = parseVerbOfOperation(operation, LgVerbs[i]);
						 if(opWoVerb){
						 	parsedTweetflow.verb = LgVerbs[i];
						 	parsedTweetflow.didOperation = opWoVerb;
						 	break;
						 }
					};
					
					// if no verb match just use the operation without "did"
					if(!parsedTweetflow.verb){
						parsedTweetflow.didOperation=os[0].substr(3);
					}
		   		}
		   		parsedTweetflow.service=os[1];
		   }
		}
	}
	
	/// check if operation starts with verb
	/// returns operation without verb
	parseVerbOfOperation = function(operation, verb){
		if(operation.toLowerCase().indexOf(verb.toLowerCase()) == 0){
			return operation.substr(verb.length);
		}
		return false;
	}
	
	parseOutput = function(text){
		if(text && typeof(text)=='string'){
			parsedTweetflow.output = text;
		}
	}
	
	parseHashtags = function(){
		for (var i=0; i < words.length; i++) {
		  if(typeof(words[i])=='string' && words[i].charAt(0)=='#'){
		  	// this is a hashtag!
		  	if(parsedTweetflow.hashtags && parsedTweetflow.hashtags instanceof Array){
		  		parsedTweetflow.hashtags.push(words[i]);
		  	}
		  	else{
		  		parsedTweetflow.hashtags = new Array(words[i]);
		  	}
		  }
		};
	}
	
	parseWords();
	determineType();
	
	if(parsedTweetflow && parsedTweetflow.type){
		if(parsedTweetflow.type==SR || parsedTweetflow.type==RT || parsedTweetflow.type==DS){
			parseSR();
		}
		else if(parsedTweetflow.type==LG){
			parseLG();
		}
		else if(parsedTweetflow.type==SF){
			parseSF();
		}
		
		parseHashtags();
		
		if(!parsedTweetflow.type){
			// it turned out that this cant be a tweetflow because something
			// went wrong during parsing
			parsedTweetflow=undefined;
		}
	}
	
	
	
	return parsedTweetflow;	
}

